package com.example.zxing.utils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.example.zxing.R;
import com.example.zxing.utils.osbar.OsBarUtil;
import com.example.zxing.utils.osbar.StatusBarUtil;

import client.PhotoClient;
import client.PhotoSelectResultBean;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

public class ZxingQrcodeActivity extends AppCompatActivity implements QRCodeView.Delegate, View.OnClickListener {
    private static final String TAG = ZxingQrcodeActivity.class.getSimpleName();
    private ZXingView mZXingView;
    private View viewPower, viewBack;
    private LinearLayout lineRight,lineLeft;
    private TextView tvBack, tvTitle,tvRight;
    private RelativeLayout barTop;
    private boolean isFlashLight = false;
    private Activity activity;

    public static ZxingLiveEvent<ZxingResultBean> qrcodeResult = new ZxingLiveEvent<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zxing_qrcode);

        barTop = findViewById(R.id.rey_zxing_top);
        mZXingView = findViewById(R.id.zxingview);
        viewPower = findViewById(R.id.view_zxing_power);
        viewBack = findViewById(R.id.view_zxing_back);
        tvBack = findViewById(R.id.tv_zxing_back);
        tvTitle = findViewById(R.id.tv_zxing_title);
        tvRight=findViewById(R.id.tv_zxing_right);
        lineRight = findViewById(R.id.line_zxing_right);
        lineLeft = findViewById(R.id.line_zxing_left);
        activity=this;

        init();
    }

    private void init() {
        lineLeft.setOnClickListener(this);
        barTop.setBackgroundColor(ZxingClient.getClient().getBarBgColor());
        if(ZxingClient.getClient().isToOsBar()){
            OsBarUtil.setStatusBarColor(this, ZxingClient.getClient().getBarTextColor(),ZxingClient.getClient().getBarBgColor());
        }
        viewBack.setBackgroundResource(ZxingClient.getClient().getImgBack());
        //返回键
        if (ZxingClient.getClient().isShowBackText()) {
            tvBack.setText(ZxingClient.getClient().getBarBackText());
            tvBack.setVisibility(View.VISIBLE);
            tvBack.setTextColor(ZxingClient.getClient().getBarTextColor());
        } else {
            tvBack.setVisibility(View.INVISIBLE);
        }
        //标题
        tvTitle.setText(ZxingClient.getClient().getBarTitleText());
        tvTitle.setTextColor(ZxingClient.getClient().getBarTextColor());
        //右部相册
        if(ZxingClient.getClient().isOpenPhoto()){
            lineRight.setVisibility(View.VISIBLE);
            tvRight.setTextColor(ZxingClient.getClient().getBarTextColor());
            lineRight.setOnClickListener(this);
        }else{
            lineRight.setVisibility(View.INVISIBLE);
        }
        //设置扫描
        mZXingView.getScanBoxView().setQRCodeTipText(ZxingClient.getClient().getScanTipText());

        mZXingView.getScanBoxView().setCornerColor(ZxingClient.getClient().getScanCornerColor());
        mZXingView.getScanBoxView().setBorderColor(ZxingClient.getClient().getScanBorderColor());
        mZXingView.getScanBoxView().setScanLineColor(ZxingClient.getClient().getScanLineColor());
        mZXingView.getScanBoxView().setTipTextColor(ZxingClient.getClient().getScanTipTextColor());
        mZXingView.getScanBoxView().setIsBarcode(ZxingClient.getClient().isBarcode());
        mZXingView.getScanBoxView().setAutoZoom(ZxingClient.getClient().isAutoZoom());
        //mZXingView.getScanBoxView().setOnlyDecodeScanBoxArea(false); // 识别整个屏幕中的码

        //其他设置
        viewPower.setOnClickListener(this);
        mZXingView.setDelegate(this);
    }

    @Override
    protected void onStart() {
        super.onStart();

        mZXingView.startCamera(); // 打开后置摄像头开始预览，但是并未开始识别
//        mZXingView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT); // 打开前置摄像头开始预览，但是并未开始识别
        mZXingView.startSpotAndShowRect(); // 显示扫描框，并开始识别
    }

    @Override
    protected void onStop() {
        mZXingView.stopCamera(); // 关闭摄像头预览，并且隐藏扫描框
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mZXingView.onDestroy(); // 销毁二维码扫描控件
        super.onDestroy();
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        Log.e(TAG, "result:" + result);
        if (qrcodeResult.hasObservers()) {
            qrcodeResult.postValue(new ZxingResultBean(ZxingClient.getClient().getZxingTag(),result));
        }
        finish();
    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {
        // 这里是通过修改提示文案来展示环境是否过暗的状态，接入方也可以根据 isDark 的值来实现其他交互效果
        String tipText = mZXingView.getScanBoxView().getTipText();
        String ambientBrightnessTip = "\n环境过暗，请打开闪光灯";
        Log.e("zxing环境检测", String.valueOf(isDark));
        if (isDark) {
            if (!tipText.contains(ambientBrightnessTip)) {
                mZXingView.getScanBoxView().setTipText(tipText + ambientBrightnessTip);
                viewPower.setVisibility(View.VISIBLE);
            }
        } else {
            if (tipText.contains(ambientBrightnessTip)) {
                tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip));
                mZXingView.getScanBoxView().setTipText(tipText);
                //如果闪光灯未开的情况亮度恢复  则取消掉手电筒显示
            }

            if (!isFlashLight) {
                viewPower.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Log.e(TAG, "打开相机出错");
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.view_zxing_power) {
            if (!isFlashLight) {
                mZXingView.openFlashlight();
                isFlashLight = true;
            } else {
                mZXingView.closeFlashlight();
                isFlashLight = false;
            }
        } else if (id == R.id.line_zxing_right) {
            getPhotoQrcode();
        }else if (id == R.id.line_zxing_left) {
            finish();
        }
    }


    private void getPhotoQrcode(){
        PhotoClient.getClient().selectPhotoResult(activity, new PhotoClient.PhotoResult() {
            @Override
            public void back(PhotoSelectResultBean result) {
                Log.e("收到的图像选择回调", "Tag：" + result.getTag() + "     Result:" + result.getResult());
                if (!result.getTag().equals(activity.getClass().getName() )) {
                    return;
                }
                mZXingView.decodeQRCode(result.getResult().get(0));
            }
        });

        PhotoClient.getClient().setDefault()
                .setPhotoResultTag(activity.getClass().getName())
                .setMultiSelectPhoto(false)
                .setCropPhoto(false)
                .setTakePhoto(false)
                .setSelectPhotoList(null)
                .setToOsBar(true)
                .startPhotoPicker(activity);
    }


}